package com.atguigu.tingshu.payment.service.impl;
import java.math.BigDecimal;
import java.util.Date;

import com.alibaba.fastjson.JSON;
import com.atguigu.tingshu.common.constant.KafkaConstant;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.service.KafkaService;
import com.atguigu.tingshu.model.account.RechargeInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Maps;

import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.model.payment.PaymentInfo;
import com.atguigu.tingshu.payment.mapper.PaymentInfoMapper;
import com.atguigu.tingshu.payment.service.PaymentInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wechat.pay.java.service.payments.model.Transaction;
import jakarta.websocket.server.PathParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@SuppressWarnings({"unchecked", "rawtypes"})
@Slf4j
public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, PaymentInfo> implements PaymentInfoService {

    @Autowired
    private KafkaService kafkaService;

    @Override
    public void savePaymentInfo(OrderInfo orderInfo) {

        // 根据订单的编号查询订单数据
        LambdaQueryWrapper<PaymentInfo> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(PaymentInfo::getOrderNo , orderInfo.getOrderNo()) ;
        PaymentInfo paymentInfo = this.getOne(lambdaQueryWrapper);

        if(paymentInfo == null) {
            //创建PaymentInfo对象，封装要保存的交易信息数据
            paymentInfo = new PaymentInfo();
            paymentInfo.setUserId(orderInfo.getUserId());
            paymentInfo.setPaymentType(SystemConstant.PAYMENT_TYPE_ORDER);
            paymentInfo.setOrderNo(orderInfo.getOrderNo());
            paymentInfo.setPayWay(orderInfo.getPayWay());
            paymentInfo.setOutTradeNo(orderInfo.getOrderNo());
            paymentInfo.setAmount(orderInfo.getOrderAmount());
            paymentInfo.setContent(orderInfo.getOrderTitle());
            paymentInfo.setPaymentStatus(SystemConstant.PAYMENT_STATUS_UNPAID);
            this.save(paymentInfo);
        }

    }

    @Override
    public void savePaymentInfo(RechargeInfo rechargeInfo) {

        // 根据订单的编号查询订单数据
        LambdaQueryWrapper<PaymentInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PaymentInfo::getOrderNo,rechargeInfo.getOrderNo());
        PaymentInfo paymentInfo = this.getOne(lambdaQueryWrapper);

        if (paymentInfo ==null){
            paymentInfo = new PaymentInfo();
            paymentInfo.setUserId(rechargeInfo.getUserId());
            paymentInfo.setPaymentType(SystemConstant.PAYMENT_TYPE_RECHARGE);
            paymentInfo.setOrderNo(rechargeInfo.getOrderNo());
            paymentInfo.setPayWay(rechargeInfo.getPayWay());
            paymentInfo.setOutTradeNo(rechargeInfo.getOrderNo());
            paymentInfo.setAmount(rechargeInfo.getRechargeAmount());
            paymentInfo.setContent("充值");
            paymentInfo.setPaymentStatus(SystemConstant.PAYMENT_STATUS_UNPAID);
            this.save(paymentInfo);
        }

    }

    @Override
    public void updatePaymentStatus(Transaction transaction) {

        log.info(JSON.toJSONString(transaction));

        // 查询交易信息并且判断交易的状态
        String orderNo = transaction.getOutTradeNo();
        LambdaQueryWrapper<PaymentInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>() ;
        lambdaQueryWrapper.eq(PaymentInfo::getOrderNo , orderNo);
        PaymentInfo paymentInfo = this.getOne(lambdaQueryWrapper);
        if(SystemConstant.PAYMENT_STATUS_PAID.equals(paymentInfo.getPaymentStatus())) {     // 已支付
            return ;
        }

        // 更改交易状态
        paymentInfo.setPaymentStatus(SystemConstant.PAYMENT_STATUS_PAID);
        paymentInfo.setCallbackTime(new Date());
        paymentInfo.setCallbackContent(JSON.toJSONString(transaction));
        this.updateById(paymentInfo);

        String paymentType = paymentInfo.getPaymentType();//获取订单状态
        if (SystemConstant.PAYMENT_TYPE_ORDER.equals(paymentType)){//订单支付
            kafkaService.sendMessage(KafkaConstant.QUEUE_ORDER_PAY_SUCCESS , orderNo);
            log.info("支付成功，发送更改订单状态消息成功了....");
        }else if (SystemConstant.PAYMENT_TYPE_RECHARGE.equals(paymentType)){//充值支付
            kafkaService.sendMessage(KafkaConstant.QUEUE_RECHARGE_PAY_SUCCESS, orderNo);
            log.info("支付成功，发送更改充值订单状态消息成功了....");
        }

    }
}
